package com.javasm.crm.controller.aftersale;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javasm.crm.common.http.AxiosResult;
import com.javasm.crm.controller.base.BaseController;
import com.javasm.crm.domin.criteria.aftesale.RefacInApplyCriteria;
import com.javasm.crm.domin.entity.aftersale.RefacInApply;
import com.javasm.crm.domin.entity.aftersale.RefacInGoods;
import com.javasm.crm.domin.entity.aftersale.RefacOutApply;
import com.javasm.crm.domin.vo.aftersale.RefacInApplyVO;
import com.javasm.crm.service.aftersale.IRefacInApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import sun.dc.pr.PRError;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lirui
 * @since 2021-07-11
 */
@RestController
@RequestMapping("/crm/refacinapply")
public class RefacInApplyController extends BaseController {

    @Resource
    private IRefacInApplyService inApplyService;

    @GetMapping("find")
    @ApiOperation(value = "查询入库申请单", notes = "条件、分页均可")
    public AxiosResult find(RefacInApplyCriteria inApplyCriteria) {
        Page<RefacInApply> page = inApplyService.getPage(inApplyCriteria);

        return AxiosResult.success(page);
    }

    @GetMapping("getInApplyById")
    @ApiOperation(value = "根据id获取出库申请单", notes = "根据id获取出库申请单")
    public AxiosResult getInApplyById(Integer id) {

        RefacInApply inApply = inApplyService.getById(id);

        System.out.println(inApply.toString());

        return AxiosResult.success(inApply);
    }

    @PostMapping("addInApply")
    @ApiOperation(value = "新申请单", notes = "包括参数和相关的商品")
    public AxiosResult addInApply(@RequestBody RefacInApplyVO refacInApplyVO, HttpServletRequest request) {

//        Object loginUser = request.getSession().getAttribute("loginUser");
        String userName = "lee";

        boolean flag = inApplyService.addInApply(refacInApplyVO, userName);

        String message = flag ? "添加成功！" : "添加失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }

    @PostMapping("alterInApply")
    @ApiOperation(value = "修改入库申请单", notes = "包括参数和相关的商品")
    public AxiosResult alterInApply(@RequestBody RefacInApplyVO refacInApplyVO, HttpServletRequest request) {

//        Object loginUser = request.getSession().getAttribute("loginUser");

        String userName = "lee";

        boolean flag = inApplyService.alterInApply(refacInApplyVO, userName);

        String message = flag ? "修改成功！" : "修改失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }

    @DeleteMapping("batchDelete")
    @ApiOperation(value = "要删除的入库申请单id集合", notes = "同时修改对应出库申请单的状态status=1,已出库")
    public AxiosResult batchDelete(@RequestBody List<Serializable> idList){

        boolean flag = inApplyService.batchDelete(idList);

        String message = flag ? "删除成功！" : "删除失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }

    @PostMapping("approveInApply")
    @ApiOperation(value = "入库申请审核", notes = "入库申请审核，同时更改对应的出库申请单状态")
    public AxiosResult approveInApply(@RequestBody RefacInApply refacInApply, HttpServletRequest request){

//        Object loginUser = request.getSession().getAttribute("loginUser");

        String userName = "Vio";

        boolean flag = inApplyService.approveApply(refacInApply,userName);

        String message = flag ? "审核成功！" : "审核失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }
}
